package com.itheima.article.controller;

import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.pojo.ApAuthor;
import com.itheima.article.service.ApArticleService;
import com.itheima.article.service.ApAuthorService;
import com.itheima.common.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class ApiController {
    @Resource
    private ApAuthorService apAuthorService;
    @Resource
    private ApArticleService apArticleService;
    /**
     * 通过UserId及wmUserId查询作者信息
     * @param apUserId
     * @return
     */
    @GetMapping("/api/apAuthor/getByApUserIdWmUserId/{apUserId}/{wmUserId}")
    public ResultVo<ApAuthor> getByApUserIdWmUserId(@PathVariable(value = "apUserId") Long apUserId,
                                             @PathVariable(value = "wmUserId") Long wmUserId){
        ApAuthor author= apAuthorService.getByApUserIdWmUserId(apUserId,wmUserId);
        return ResultVo.ok(author);
    }

    /**
     * 添加作者账号
     * @param apAuthor
     * @return
     */
    @PostMapping("/api/apAuthor/add")
    public ResultVo addApAuthor(@RequestBody ApAuthor apAuthor){
        apAuthorService.save(apAuthor);
        return ResultVo.ok("操作成功");
    }

    /**
     * 同步自媒体文章到文章微服
     * @param dto
     * @return
     */
    @PostMapping("/api/apArticle/saveArticleInfo")
    public ResultVo saveArticleInfo(@RequestBody ArticleInfoDto dto){
        apArticleService.saveArticleInfo(dto);
        return ResultVo.ok();
    }

    /**
     * 通过文章id获取作者名称
     * @param articleId
     * @return
     */
    @GetMapping("/api/apArticle/getAuthorName/{articleId}")
    public ResultVo<String> getAuthorNameByArticleId(@PathVariable(value = "articleId") Long articleId){
        ApArticle apArticle = apArticleService.getById(articleId);
        Object authorName = apArticle.getAuthorName();
        return ResultVo.ok(authorName);
    }
}
